package com.catchman.config;

import com.catchman.quartz.CMJobFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @author catchman
 */
@Configuration
public class QuartzSchedulerConfig {
    @Bean("CatchManQuartzScheduler")
    public Scheduler getScheduler(SchedulerFactoryBean schedulerFactoryBean) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        // Scheduler will not execute jobs until it has been started (though they can be scheduled before start())
        scheduler.start();
        return scheduler;
    }

    @Bean
    public CMJobFactory jobFactory() {
        return new CMJobFactory();
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CMJobFactory jobFactory) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jobFactory);
        return schedulerFactoryBean;
    }


}
